﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Empires.CRL.Tools;
using Empires.CRL.DataContract;
using System.Web;
using System.IO;

namespace Empires.BLL.ServerProvider
{
    public static class Tools
    {
        /// <summary>
        /// 返回扩展名
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static string GetExtendName(string filename)
        {
            Regex reg = new Regex(CommonRegexpLib.GetExtendName);
            Match match = reg.Match(filename);
            if (match.Success)
            {
                var ext = match.Groups["ext"];
                return ext != null ? ext.Value : string.Empty;
            }
            return string.Empty;
        }
        /// <summary>
        /// 文件上传,如果上传失败，键为input:file:name；值为String.Empty
        /// </summary>
        /// <param name="files"></param>
        /// <param name="upload"></param>
        /// <returns></returns>
        public static Dictionary<string,string> UploadFiles(List<UploadFileDataPack> files, HttpFileCollection upload)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            HttpPostedFile postFile;
            string fileType = string.Empty;
            string fileName = string.Empty;
            Regex customerName = new Regex(CommonRegexpLib.CustomerName);

            string uploadPath = string.Empty; //需要上传到的路径
            foreach (var item in files)
            {
                if (!String.IsNullOrEmpty(item.UploadName) && !String.IsNullOrEmpty(item.BasePath))
                {
                    try
                    {
                        postFile = upload[item.UploadName];

                        if (postFile != null && postFile.ContentLength < 5242880) //如果存在该上传文件切小于5MB
                        {
                            fileName = postFile.FileName;
                            fileType = GetExtendName(fileName);
                            if (item.TypeFilter.Exists(c=>c == FileType.All) || item.TypeFilter.Exists(c => c.ToString() == fileType)) //如果该文件扩展名为合法
                            {
                                uploadPath = item.BasePath.Replace("\\", "/");
                                uploadPath = uploadPath.EndsWith("/") ? uploadPath : uploadPath + "/";
                                if (!String.IsNullOrEmpty(item.CustomerFileName))
                                {
                                    item.CustomerFileName = customerName.Replace(item.CustomerFileName, "");
                                    if (!String.IsNullOrEmpty(item.CustomerFileName))
                                    {
                                        item.CustomerFileName = item.CustomerFileName.StartsWith("/") ? item.CustomerFileName.Substring(1) : item.CustomerFileName;
                                        uploadPath += item.CustomerFileName;
                                    }
                                }
                                else
                                {
                                    string name = new FileInfo(fileName).Name;
                                    uploadPath += name;
                                }
                                string s = HttpContext.Current.Server.MapPath(uploadPath);
                                s = GetNewName(fileName, item, s);
                                postFile.SaveAs(s);

                                result.Add(item.UploadName, uploadPath);
                            }
                        }
                    }
                    catch 
                    {
                        result.Add(item.UploadName, String.Empty);
                    }
                }
                else
                {
                    result.Add(item.UploadName, String.Empty);
                }
            }
            return result;
        }
        /// <summary>
        /// 如果现在的复制模式为rename模式，则为该文件选择一个未重复的名字
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="item"></param>
        /// <param name="s"></param>
        /// <param name="circle"></param>
        /// <returns></returns>
        private static string GetNewName(string fileName, UploadFileDataPack item, string s, int circle = 1)
        {
            if (item.CopyMode == CopyMode.cover)
            {
                return s;
            }
            if (System.IO.File.Exists(s))
            {
                var fileInfo = new FileInfo(fileName);
                string name = fileInfo.Name;
                s = s.Replace(name, name.Substring(0, name.LastIndexOf('.')) + circle++ + fileInfo.Extension);
                s = GetNewName(s, item, s, circle);
            }
            return s;
        }

    }
}
